<template>
  <div>
    <div class="card" style="margin-bottom: 10px;">
      <el-input v-model="data.keyword" style="width: 300px; margin-right: 10px" placeholder="请输入关键字查询"></el-input>
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button type="info" style="margin: 0 10px" @click="handleReset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 10px">
      <div style="margin-bottom: 10px">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-table :data="data.tableData">
        <el-table-column prop="id" label="题号" width="100"></el-table-column>
        <el-table-column prop="question" label="题目" width="300"></el-table-column>
        <el-table-column prop="optiona" label="选项A"></el-table-column>
        <el-table-column prop="optionb" label="选项B"></el-table-column>
        <el-table-column prop="resulta" label="结果A" width="100"></el-table-column>
        <el-table-column prop="resultb" label="结果B" width="100"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>

  <el-dialog v-model="data.formVisible" title="添加题目信息" width="500">
    <el-form :model="data.form2" label-width="120px">
      <el-form-item label="题号">
        <el-input v-model="data.form2.id" autocomplete="off" />
      </el-form-item>
      <el-form-item label="题目">
        <el-input v-model="data.form2.question" autocomplete="off" />
      </el-form-item>
      <el-form-item label="选项A">
        <el-input v-model="data.form2.optiona" autocomplete="off" />
      </el-form-item>
      <el-form-item label="选项B">
        <el-input v-model="data.form2.optionb" autocomplete="off" />
      </el-form-item>
      <el-form-item label="结果A">
        <el-input v-model="data.form2.resulta" autocomplete="off" />
      </el-form-item>
      <el-form-item label="结果B">
        <el-input v-model="data.form2.resultb" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="data.formVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确认</el-button>
      </div>
    </template>
  </el-dialog>

    <el-dialog v-model="data.updateformVisible" title="修改题目信息" width="500">
      <el-form :model="data.form1" label-width="120px">
        <el-form-item label="题目">
          <el-input v-model="data.form1.question" autocomplete="off" />
        </el-form-item>
        <el-form-item label="选项A">
          <el-input v-model="data.form1.optiona" autocomplete="off" />
        </el-form-item>
        <el-form-item label="选项B">
          <el-input v-model="data.form1.optionb" autocomplete="off" />
        </el-form-item>
        <el-form-item label="结果A">
          <el-input v-model="data.form1.resulta" autocomplete="off" />
        </el-form-item>
        <el-form-item label="结果B">
          <el-input v-model="data.form1.resultb" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="data.updateformVisible = false">取消</el-button>
          <el-button type="primary" @click="update">确认</el-button>
        </div>
      </template>
    </el-dialog>

</template>

<script setup>
import {reactive} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus';
import request from '@/utils/request';

const data = reactive({
  keyword: '',
  tableData: [],
  formVisible: false,
  form1:{},
  form2:{}
})

const handleSearch = () =>{
  request.get('/questions/listQuestion',
      {
        params: {
          keyword: data.keyword
        }
      }).then(res => {
    data.tableData = res.data || []
  }).catch(error => {
    console.error('Error fetching data:', error); // 输出错误信息
  });
}
//调用方法获取后台数据
handleSearch()
const handleAdd = () => {
  // 实现新增功能
  data.form2={}
  data.formVisible = true
};

const handleReset = () => {
  data.keyword = ''; // 重置关键字为空
  handleSearch();
};
const save = () => {
  request.post('/questions/addQuestion',data.form2).then(res => {
    if(res.code === '200') {
      handleSearch()   //重新获取数据
      data.formVisible = false
      ElMessage.success("操作成功")
    }else {
      ElMessage.error(res.msg)
    }
  })
}

const update = () => {
  request.put( '/questions/updateQuestion', data.form1).then(res => {
    if(res.code === '200') {
      handleSearch()   //重新获取数据
      data.updateformVisible = false
      ElMessage.success("操作成功")
    }else {
      ElMessage.error(res.msg)
    }
  })
}
//
const handleEdit = (row) => {
  // 实现编辑功能
  console.log('Clicked edit button')
  data.form1 =JSON.parse(JSON.stringify(row))
  data.updateformVisible = true
}
const handleDelete = (id) => {
  // 实现删除功能
  ElMessageBox.confirm('确定删除该题目信息吗？', '删除确认', {type: 'warning'}).then(res =>{
    request.delete('/questions/deleteQuestion/' + id).then(res => {
      if(res.code === '200') {
        handleSearch()   //重新获取数据
        ElMessage.success("删除成功")
      }else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(res => {
    ElMessage({
      type: 'info',
      message: '取消删除'
    })
  })
};
</script>

